package gui;

import dao.database;
import gui.card.*;
import gui.consumption.add_Consumption;
import gui.consumption.add_money;
import gui.consumption.static_info;
import pojo.Card;
import pojo.Consumption;
import pojo.ConsumptionWithName;
import service.cardService;
import service.consumptionService;
import util.JTableDataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class mainWindow {
    private JTabbedPane tabbedPane1;
    private JPanel panel1;
    private JButton seal_card_Button;
    private JButton 查询会员Button;
    private JButton 会员禁用Button;
    private JButton 退卡Button;
    private JButton 添加消费Button;
    private JButton 存款Button;
    private JButton 按照id查询消费情况Button;
    private JButton 修改密码Button;
    private JTable table1;
    private JButton consumption_fresh_Button;
    private JTable card_table2;
    private JButton 刷新Button;
    private JTextField query_user_id;
    private JButton 会员启用Button;
    private JTextField textField1;
    private JButton 统计信息Button;

    public mainWindow() {
        query_consumption();
        query_card();

        seal_card_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                card_sell_window.show();

            }
        });
        查询会员Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                queryCard.show();
            }
        });
        会员禁用Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                stop_Card.show();

            }
        });
        退卡Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                del_Card.show();
            }
        });
        存款Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

            }
        });
        添加消费Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                add_Consumption.show();
            }
        });
        consumption_fresh_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                query_consumption();
            }
        });
        刷新Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                query_card();
            }
        });
        存款Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                add_money.show();
            }
        });
        按照id查询消费情况Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                query_consumption_by_id();
            }
        });
        会员启用Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                start_Card.show();
            }
        });
        修改密码Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                database d = new database();
                String sql = "UPDATE swim.user t\n" +
                        "SET t.passwd = '" + textField1.getText() +
                        "'\n" +
                        "WHERE t.id = 1;";
                if (d.update_execute(sql) == 1) {
                    alert.show("修改成功");
                } else {
                    alert.show("修改失败");
                }
            }
        });
        统计信息Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                static_info.main();
            }
        });
    }

    public void query_consumption() {
        final String[] columnNames = {"会员卡id", "序号", "用户名", "金额", "消费内容"};
        List<ConsumptionWithName> list = consumptionService.all();
        JTableDataUtil<ConsumptionWithName> jTableDataUtil = new JTableDataUtil<>(ConsumptionWithName.class);
        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(list), columnNames);
        table1.setModel(dataModel);
    }

    public void query_consumption_by_id() {
        if (cardService.queryByid(Integer.parseInt(query_user_id.getText())) == null) {
            query_consumption_by_id_de();
        } else {
            final String[] columnNames = {"会员卡id", "序号", "用户名", "金额", "消费内容"};
            List<ConsumptionWithName> list = consumptionService.queryByid(Integer.parseInt(query_user_id.getText()));
            JTableDataUtil<ConsumptionWithName> jTableDataUtil = new JTableDataUtil<>(ConsumptionWithName.class);
            TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(list), columnNames);
            table1.setModel(dataModel);
        }

    }

    public void query_consumption_by_id_de() {
        final String[] columnNames = {"序号", "会员卡id", "金额", "消费内容"};
        List<Consumption> list = consumptionService.queryByid_delete(Integer.parseInt(query_user_id.getText()));
        JTableDataUtil<Consumption> jTableDataUtil = new JTableDataUtil<>(Consumption.class);
        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(list), columnNames);
        table1.setModel(dataModel);
    }

    public void query_card() {
        final String[] columnNames = {"会员卡id", "金额", "折扣率", "类型", "用户名", "电话", "身份证号", "是否可用", "停用信息"};
        List<Card> list = cardService.all();
        JTableDataUtil<Card> jTableDataUtil = new JTableDataUtil<>(Card.class);
        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(list), columnNames);
        card_table2.setModel(dataModel);
    }

    public void show() {
        JFrame frame = new JFrame("游泳馆管理系统");
        frame.setContentPane(new mainWindow().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        seal_card_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
            }
        });
    }
}
